<?php

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$currentDir = dirname(__FILE__);
require_once($currentDir . "/sensapilityservice.php");
require_once($currentDir . "/utilities.php");

/**
 * @author api.jmatt@gmail.com
 * @version 3.0
 */
class SensapilityChannel extends SensapilityService{

  /**
   * @param soapclientnusoap $soapClient the NuSOAP object that communicates 
   * with the service
   * @param array $namespaces associative array that matches namespace labels
   * to namespace URIs
   * @param array $defaultNamespaceLabel the namespace to be used with most
   * tags
   * @return none
   */
  function SensapilityChannel($soapClient, 
                              $namespaces, 
                              $defaultNamespaceLabel) {
    parent::SensapilityService($soapClient,
                               $namespaces,
                               $defaultNamespaceLabel);
  }

  /**
   * Creates a new URL or custom channel.
   * @param array $channel An associative array formatted like a Channel_Data object.
   * Described 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html here}.
   * @return Channel_Data A Channel_Data object with the channel id set
   */
  function createChannel($channel) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':id' => -1,
                            $ns . ':name' => $channel['name'],
                            $ns . ':status' => array('value' =>
                                                 array('value' =>
                                                   $channel['status'])),
                            $ns . ':synServiceId' => $channel['synServiceId'],
                            $ns . ':type' => array('value' =>
                                               array('value' =>
                                                 $channel['type'])));
    $callParameters = array($ns . ':channel' => 
                              array("value" => $callParameters));

    return parent::doSoapCall('createChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Create multiple channels in one request
   * @param array $channelArray An array of associative arrays formatted like 
   * Channel_Data objects
   * @return array An array of Channel_Data objects 
   */
  function createChannels($channelArray) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";

    $channels = array_keys($channelArray);
    foreach ( $channels as $thisChannel ) {
      $channelObj = $channelArray[$thisChannel];
      $singleChannel = array($ns . ':id' => -1,
                             $ns . ':name' => $channelObj['name'],
                             $ns . ':status' => array('value' => 
                                                  array('value' => 
                                                    $channelObj['status'])),
                             $ns . ':synServiceId'=>$channelObj['synServiceId'],
                             $ns . ':type' => array('value' =>
                                                array('value' => $channelObj['type'])));
      $singleChannel = array($ns . ':channel' =>array("value"=>$singleChannel));
      $callParameters .= ArrayToXML($singleChannel);
    }
    
    return parent::doSoapCall('createChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * Removes the specified channel
   * @param long $killTarget The id of the channel to be removed.
   * @return none
   */ 
  function deleteChannel($killTarget) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':channelId' => $killTarget);

    return parent::doSoapCall('deleteChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Removes specified channels
   * @param array $killTargets an array of longs which are channel ids to 
   * remove
   * @return none
   */
  function deleteChannels($killTargets) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";
    $keys = array_keys($killTargets);
    foreach ( $keys as $key ) {
      $tempArray = array($ns . ':channelIds' => $killTargets[$key]);
      $callParameters .= ArrayToXML($tempArray);
    }

    return parent::doSoapCall('deleteChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Retrieves information for specified channel
   * @param long $channelId The numeric identifier for this channel
   * @return Channel_Data a Channel_Data object that represents the requested 
   * channel
   **/
  function getChannel($channelId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':channelId' => $channelId);

    return parent::doSoapCall('getChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Retrieves information for specified channels
   * @param array $channelIds Array of numeric identifiers for channels
   * @return array Array of Channel_Data objects for the channels requested
   */
  function getChannels($channelIds) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";
    $keys = array_keys($channelIds);
    foreach ( $keys as $key ) {
      $tempArray = array($ns . ':channelIds' => $channelIds[$key]);
      $callParameters .= ArrayToXML($tempArray);
    }

    return parent::doSoapCall('getChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * Gets the service a given channel belongs to
   * @param long $channelId The numeric ID of the channel in question
   * @return SyndicationService_Data A SyndicationService_Data object for the
   * service that this channel belongs to.
   */
  function getSyndicationService($channelId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':channelId' => $channelId);

    return parent::doSoapCall('getSyndicationService',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * Update attributes of a channel
   * @param array $channel An associative array formatted like a Channel_Data object.
   * Described 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html here}.
   * @return none
   */
  function updateChannel($channel) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':id' => $channel['id'],
                            $ns . ':name' => $channel['name'],
                            $ns . ':status' => array('value' =>
                                                 array('value' =>
                                                   $channel['status'])),
                            $ns . ':synServiceId' => $channel['synServiceId'],
                            $ns . ':type' => array('value' =>
                                               array('value' =>
                                                 $channel['type'])));
    $callParameters = array('channel' => array('value' => $callParameters));

    return parent::doSoapCall('updateChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * The same as update channel, but updates multiple channels in a single call
   * @param array $channelArray Array of Channel_Data objects with the
   * update information
   * @return none
   */
  function updateChannels($channelArray) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";

    $channels = array_keys($channelArray);
    foreach ( $channels as $thisChannel ) {
      $channelObj = $channelArray[$thisChannel];
      $singleChannel = array($ns . ':id' => $channelObj['id'],
                             $ns . ':name' => $channelObj['name'],
                             $ns . ':status' => array('value' =>
                                                  array('value' =>
                                                    $channelObj['status'])),
                             $ns . ':synServiceId'=>$channelObj['synServiceId'],
                             $ns . ':type' => array('value' =>
                                                array('value' =>
                                                  $channelObj['type'])));
      $singleChannel = array($ns .':channels' =>
                                 array("value"=>$singleChannel) );
      $callParameters .= ArrayToXML($singleChannel);
    }

    return parent::doSoapCall('updateChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }
  
  /**
   * Retrieve the channel code for channel. Channel codes are used by 
   * publishers to identify AdSense channels, whereas developers use 
   * ChannelService_Data.id to identify a channel within the API.
   * @param array $channel.  An associative array comprising a 
   * ChannelService_Data object.
   * @return string the channel code in the Channels table
   */
  function getChannelCode($channel) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':id' => -1,
                            $ns . ':name' => $channel['name'],
                            $ns . ':status' => array('value' =>
                                                 array('value' =>
                                                   $channel['status'])),
                            $ns . ':synServiceId' => $channel['synServiceId'],
                            $ns . ':type' => array('value' =>
                                               array('value' =>
                                                 $channel['type'])));
    $callParameters = array('channel' => array('value' => $callParameters));

    return parent::doSoapCall('getChannelCode',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
    
  }
}
?>
